package wangm.netty.test1;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;

/**
 * description
 *
 * @author zk
 * @since 2018/7/20
 */
public class DiscardInboundHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        String message = new String (ByteBufUtil.getBytes((ByteBuf) msg),"UTF-8");
        ReferenceCountUtil.release(msg);
        System.out.println("server rec: " + message);
        ctx.channel().writeAndFlush(Unpooled.wrappedBuffer(("you mssage is : " + message).getBytes())).addListener((future -> {
            System.out.println("server has send");
        }));
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
//        ByteBuf byteBuf = ctx.alloc().buffer(4);
//        byteBuf.writeInt((int)(System.currentTimeMillis() / 1000L + 2208988800L));
//
//        ChannelFuture channelFuture = ctx.writeAndFlush(byteBuf);
//        channelFuture.addListener((future)->{assert future == channelFuture;ctx.close();});
    }
}
